﻿<?php
	/*define('LIVESOUL', true);
	$path="../";
	include("../common.php");
	$userdata = session_pagestart($user_ip, 'json');*/
	
	require_once("../includes/function_rp.php");
	$vehicle_status = isset($_POST['vehicle_status']) ? $_POST['vehicle_status'] : $_GET['vehicle_status'];
	//$color_status = isset($_POST['color_status']) ? $_POST['color_status'] : $_GET['color_status'];
	$start_time = isset($_POST['start_time']) ? $_POST['start_time'] : $_GET['start_time'];
	$stop_time = isset($_POST['stop_time']) ? $_POST['stop_time'] : $_GET['stop_time'];
	$distance = isset($_POST['distance']) ? $_POST['distance'] : $_GET['distance'];
	$maxspeed = isset($_POST['maxspeed']) ? $_POST['maxspeed'] : $_GET['maxspeed'];
	$box_id=isset($_POST['box_id']) ? $_POST['box_id'] : $_GET['box_id'];
	$plate = isset($_POST['plate']) ? $_POST['plate'] : $_GET['plate'];
	$location = isset($_POST['locate']) ? $_POST['locate'] : $_GET['locate'];
	$platename = isset($_POST['plate_name']) ? $_POST['plate_name'] : $_GET['plate_name'];
	$output = isset($_POST['output']) ? $_POST['output'] : $_GET['output'];
	$lang_check = isset($_POST['lang']) ? $_POST['lang'] : $_GET['lang'];
	
	$start_time=date("Y-m-d H:i:s",strtotime($start_time));
	$stop_time=date("Y-m-d H:i:s",strtotime($stop_time));

	$time_diff=strtotime($stop_time) - strtotime($start_time);

	$dbblog="host=172.30.13.3 port=5423 dbname=boxlog user=postgres password=";
	$dbconn_blog=pg_connect($dbblog);
	$sql="select CASE WHEN box_gps_time='1970-01-01 00:00:01' THEN box_server_time ELSE box_gps_time END as box_gps_time, box_gps_status, box_latitude, box_longitude from box_".$box_id;
	$sql.=" where (box_gps_time='1970-01-01 00:00:01' and box_type=0 and box_server_time>='".$start_time."' and box_server_time<='".$stop_time."') or (box_gps_time>='".$start_time."' and box_gps_time<='".$stop_time."') order by box_gps_time";

	$result=pg_query($dbconn_blog,$sql);
		if(pg_num_rows($result)>0){
			$string_latlon="<script type=\"text/javascript\">";
			$string_latlon.="function initialize() {";
			$string_latlon.="if (GBrowserIsCompatible()) {";
			$string_latlon.="var map = new GMap2(document.getElementById('map_canvas'));";
			$string_latlon.="map.addControl(new GLargeMapControl());";
			$string_latlon.="map.addControl(new GMapTypeControl());";
			$string_latlon.="var startIcon = new GIcon(G_DEFAULT_ICON);";
			$string_latlon.="var stopIcon = new GIcon(G_DEFAULT_ICON);";
			$string_latlon.="startIcon.image = './icon/icon-dd-play-trans.png';";
  			$string_latlon.="stopIcon.image = './icon/icon-dd-stop-trans.png';";
			$string_latlon.="startIcon.transparent = './icon/icon-dd-play-trans.png';";
  			$string_latlon.="stopIcon.transparent = './icon/icon-dd-stop-trans.png';";
			$string_latlon.="startIcon.printImage = './icon/icon-dd-play-trans.png';";
  			$string_latlon.="stopIcon.printImage = './icon/icon-dd-stop-trans.png';";

			$string_latlon.="marker_start = { icon:startIcon };";
			$string_latlon.="marker_stop = { icon:stopIcon };";

			$record=pg_fetch_array($result,0,PGSQL_BOTH);
			$string_latlon.="var point_start = new GLatLng(".$record["box_latitude"].", ".$record["box_longitude"].");";
			$string_latlon.="var polyline = new GPolyline([";
			for($i=0;$i<pg_num_rows($result);$i++){
				$record=pg_fetch_array($result,$i,PGSQL_BOTH);
				if((pg_num_rows($result)-$i)==1) $string_latlon.="new GLatLng(".$record["box_latitude"].", ".$record["box_longitude"].")";
				else $string_latlon.="new GLatLng(".$record["box_latitude"].", ".$record["box_longitude"]."),";
			}
			if($vehicle_status=="move" || $vehicle_status=="เดินทาง") $string_latlon.="], '#008000', 5, 0.7);";			elseif($vehicle_status=="overspeed" || $vehicle_status=="ขับเร็วเกินกำหนด") $string_latlon.="], '#FF0000', 5, 0.7);";
			//$string_latlon.="], '#FF0000', 5, 0.7);";
			$record=pg_fetch_array($result,pg_num_rows($result)-1,PGSQL_BOTH);
			$string_latlon.="var point_stop = new GLatLng(".$record["box_latitude"].", ".$record["box_longitude"].");";
			$string_latlon.="var bounds = polyline.getBounds();";
			$string_latlon.="var center = bounds.getCenter();";
			$string_latlon.="var level = map.getBoundsZoomLevel(bounds);";
			$string_latlon.="map.setCenter(center, level);";
			$string_latlon.="map.addOverlay(polyline);";
			$string_latlon.="map.addOverlay(new GMarker(point_start, marker_start));";
			$string_latlon.="map.addOverlay(new GMarker(point_stop, marker_stop));";
			
			//$string_latlon.="map.setCenter(point, 13);";
			$string_latlon.="}";
			$string_latlon.="}";  
			$string_latlon.="</script>";
		}pg_free_result($result);
	pg_close($dbconn_blog);
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
	<?php 
			if($lang_check=="th") print "แสดงแผนที่";
			else print "View Map";
	?>
	</title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAYRo9VK60DzqRHsBkppaFlRS_eW2k_STNLchqXsLMWCvAXNHBZRT_b4mM5Ff4mjtkoKAiF91eUDVAvg" type="text/javascript"></script>
	<?php
		print $string_latlon;
	?>
	<style type="text/css">
      
      body {
        font-family: Verdana, Arial, sans serif;
        font-size: 12px;
        margin: 2px;
      }
      table.directions th {
	background-color:#FFCC00;
      }
	  
      img {
        color: #000000;
      }
	 DIV.body
		{
			BORDER-RIGHT: #808080 2px solid;
			PADDING-RIGHT: 1px;
			BORDER-TOP: #808080 2px solid;
			PADDING-LEFT: 1px;
			PADDING-BOTTOM: 1px;
			BORDER-LEFT: #808080 2px solid;
			COLOR: #000000;
			PADDING-TOP: 1px;
			BORDER-BOTTOM: #808080 2px solid;
			font-family: Tahoma, Verdana, Arial, sans serif, Tahoma;
			font-size: 14px;
			margin: 2px;
			HEIGHT: 80px;
			BACKGROUND-COLOR: #ffffff;
			TEXT-DECORATION: none
		}

    </style>
    <!--<script type="text/javascript">
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(12.944657, 101.312727), 13);
        var polyline = new GPolyline([
		  new GLatLng(12.944657, 101.312727),
  		  new GLatLng(12.941087, 101.313657),
  		  new GLatLng(12.937767, 101.314517),
		  new GLatLng(12.933713, 101.315572)
		], "#0000ff", 5);
		map.addOverlay(polyline);
      }
    }  
    </script>-->
  </head>
  <body onload="initialize()" onunload="GUnload()">
  <?php
		print"<table width=\"500\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\">";
		print"<tr>";
		if($lang_check=="th"){
			//echo"<h2>".iconv("TIS-620","UTF-8","แสดงแผนที่")."</h2>";
			echo"<td><font style=\" font-family: Tahoma,Verdana, Arial, sans serif; font-size: 16px;\"><b>แสดงแผนที่</b></font></td>";
			echo"<td width=\"90\">&nbsp;</td>";
			echo"<td align=\"left\"><font style=\" font-family: Tahoma,Verdana, Arial, sans serif; font-size: 12px;\"><b>ทะเบียน</b>&nbsp;".$plate." , <b>ชื่อเรียกรถ</b> ".$platename."<br><b>สถานะ</b> ".$vehicle_status." , <b>ระยะทาง</b> ".$distance." กม. , <b>ความเร็ว</b> ".$maxspeed." กม./ชม.<br><b>วัน/เวลา</b> ".date("j/m/Y H:i:s",strtotime($start_time))." - ".date("j/m/Y H:i:s",strtotime($stop_time))." <b>ระยะเวลา</b> ".datetime_diff_days($time_diff)."</font></td>";
		}else{
			echo"<td><font style=\" font-family: Tahoma,Verdana, Arial, sans serif; font-size: 16px;\"><b>View Map</b></font></td>";
			echo"<td width=\"90\">&nbsp;</td>";
			echo"<td align=\"left\"><font style=\" font-family: Tahoma,Verdana, Arial, sans serif; font-size: 12px;\"><b>Plate No</b>&nbsp;".$plate." , <b>Vehicle Name</b> ".$platename."<br><b>Status</b> ".$vehicle_status." <b>Distance</b> ".$distance." Km <b>Speed</b> ".$maxspeed." Km/h<br><b>Date/Time</b> ".date("j/m/Y H:i:s",strtotime($start_time))." - ".date("j/m/Y H:i:s",strtotime($stop_time))." <b>Duration</b> ".datetime_diff_days($time_diff)."</font></td>";
		}
		print"</tr>";
		print"</table>";
  ?>
	<table class="directions">
	<tr>
	<?php
			if($lang_check=="th") print "<th align=\"left\">แผนที่</th>";
			else print "<th align=\"left\">Map</th>";
	?></tr>
    <tr>
    <td valign="top"><div id="map_canvas" style="width: 520px; height: 520px"></div>
	</td>
	</tr>
	</table>
  </body>
</html>